package com.ez.supervise.action.biz;

import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.Message;
import com.ez.supervise.service.MessageService;
import com.ez.supervise.util.JsonUtils;

public class MessageAction extends BaseJsonAction {
 
    private static final long serialVersionUID = 5766583445698604662L;
    private Long id;
    private MessageService messageService;
    private Message message;
    private Page page;

    public String saveMessage() {
	try {
	    if (message.isNew()) {
		messageService.insert(message);
	    } else {
		messageService.update(message);
	    }
	    Result result = new Result(message.getId());
	    result.setEntity(message);
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new",
		"content" });
	    outString(JSONObject.fromObject(result, config).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAllMessage() {
	try {
	    page = new Page();
	    List result = messageService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAll() {
	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(messageService.findAll());

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }

    public String findRootMessage() {
	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(messageService.findRootMessage());

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }
    /**
     * @return
     */
    public String deleteMessage() {
	try {
	    if (id != null && id > 0) {
		messageService.delete(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateMessage() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strMessageId = getRequest().getParameter("id");
	try {
	    if (id != null && id > 0) {
		Message saveMessage = new Message();
		saveMessage.setId(id);
		BeanUtils.setProperty(saveMessage, fieldName, fieldValue);
		messageService.update(saveMessage);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public static void main(String[] args) {
	Message message = new Message();
	message.setId(1L);
	// message.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(message);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(message, config);
	System.out.println(jsonObj.toString());
    }

    public void setMessageService(MessageService messageService) {
	this.messageService = messageService;
    }

    public Message getMessage() {
	return message;
    }

    public void setMessage(Message message) {
	this.message = message;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

}
